package com.commution.hit.service.impl;

import java.util.List;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.processor.PageProcessor;

import com.commution.hit.domain.basic.DouBanMovie;

public class DoubanMovieSpaid implements PageProcessor {
	
	private Pipeline douBanMoviePipeline;
	private Site site = Site.me().setDomain("movie.douban.com");
	@Override
	public void process(Page page) {
		List<String> links = page.getHtml().links().regex("a[href=http://movie\\.douban\\.com/subject/\\d+]").all();
        page.addTargetRequests(links);
        
        DouBanMovie bdm= new DouBanMovie();
        
        bdm.setName(page.getHtml().xpath("//div[@id='wrapper']/div[@id='content']/h1/span[@property='v:itemreviewed']/text()").toString());
        bdm.setDirect(page.getHtml().xpath("//div[@class='subject clearfix']/div[@id='info']/span/span[@class='attrs']/a[@rel='v:directedBy']/text()").toString());
        bdm.setLevel(page.getHtml().xpath("//div[@id='interest_sectl']/div[@class='rating_wrap clearbox']/p[@class='rating_self clearfix']/strong[@property='v:average']/text()").toString());
        bdm.setPhoto(page.getHtml().xpath("//div[@class='subjectwrap clearfix']/div[@class='subject clearfix']/div[@id='mainpic']/a[@class='nbgnbg']/img/@src").toString());
        page.putField("name", page.getHtml().xpath("//div[@id='wrapper']/div[@id='content']/h1/span[@property='v:itemreviewed']/text()").toString());
        page.putField("导演", page.getHtml().xpath("//div[@class='subject clearfix']/div[@id='info']/span/span[@class='attrs']/a[@rel='v:directedBy']/text()").toString());
        page.putField("评分",page.getHtml().xpath("//div[@id='interest_sectl']/div[@class='rating_wrap clearbox']/p[@class='rating_self clearfix']/strong[@property='v:average']/text()").toString());
        page.putField("海报",page.getHtml().xpath("//div[@class='subjectwrap clearfix']/div[@class='subject clearfix']/div[@id='mainpic']/a[@class='nbgnbg']/img/@src").toString());
        page.putField("DouBanMovie", bdm);
	}

	@Override
	public Site getSite() {
		return site;
	}
	public void 	start() {
		
        Spider.create(new DoubanMovieSpaid()).addUrl("http://movie.douban.com/subject/1292052")
             .addPipeline( douBanMoviePipeline).run();
    }

	public Pipeline getDouBanMoviePipeline() {
		return douBanMoviePipeline;
	}

	public void setDouBanMoviePipeline(Pipeline douBanMoviePipeline) {
		this.douBanMoviePipeline = douBanMoviePipeline;
	}

	
	
	
}
